<script>
import userService from '@/utils/userService.js';
import settingsManager from '@/store/settings.js';

export default {
	onLaunch: function() {
		console.log('App Launch');
		// 应用启动时检查token
		this.checkAndRefreshToken();
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	},
	onPageNotFound: function() {
		console.log('App Page Not Found');
	},
	methods: {
		async checkAndRefreshToken() {
			try {
				// 检查是否已登录
				if (!settingsManager.isLoggedIn) {
					return;
				}
				
				// 检查token是否需要刷新
				if (userService.checkTokenNeedsRefresh()) {
					console.log('Token needs refresh, refreshing...');
					const result = await userService.refreshToken();
					
					if (result.success) {
						console.log('Token refreshed successfully');
					} else {
						console.warn('Token refresh failed:', result.message);
						// 如果刷新失败，可能需要重新登录
						if (result.message.includes('无效') || result.message.includes('过期')) {
							console.log('Token invalid or expired, logging out...');
							settingsManager.logout();
							// 跳转到登录页
							uni.redirectTo({
								url: '/pages/user/login'
							});
						}
					}
				}
			} catch (error) {
				console.error('Error checking/refreshing token:', error);
			}
		}
	}
};
</script>

<style>
	/*每个页面公共css */
	@import './static/css/variables.css';
	@import './static/iconfont/iconfont.css';

	/* 全局样式重置 */
	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	#app {
		background-color: #f5f5f5;
		color: #333333;
		min-height: 100vh;
		font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', 'SF Pro Display', 'Helvetica Neue', Helvetica, Arial, sans-serif;
	}

	/* 确保页面继承样式 */
	page {
		background-color: #f5f5f5;
		color: #333333;
		min-height: 100vh;
		font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', 'SF Pro Display', 'Helvetica Neue', Helvetica, Arial, sans-serif;
	}

	/* 确保所有视图容器继承样式 */
	.container {
		background-color: #f5f5f5;
		color: #333333;
	}

	/* 卡片样式 */
	.card {
		background-color: #ffffff;
		color: #333333;
		border-radius: 8px;
		box-shadow: 0 1px 2px rgba(0,0,0,0.1);
	}
	
	/* iOS 18 风格按钮 */
	.ios-button {
		background-color: #007AFF;
		color: #FFFFFF;
		border-radius: 12px;
		padding: 8px 16px;
		font-weight: 500;
		text-align: center;
		transition: all 0.2s ease;
	}
	
	.ios-button:active {
		opacity: 0.8;
	}
	
	/* iOS 18 风格输入框 */
	.ios-input {
		background-color: #ffffff;
		border-radius: 8px;
		padding: 12px;
		border: 1px solid #eeeeee;
		color: #333333;
	}
	
	body {
		overscroll-behavior: none;
	}
	
	/* 平台特定样式 */
	/* #ifdef H5 */
	/* 确保H5端根元素应用样式 */
	html {
		background-color: #f5f5f5;
		color: #333333;
	}
	
	/* 自定义滚动条 */
	::-webkit-scrollbar {
		width: 8px;
		height: 8px;
	}
	
	::-webkit-scrollbar-track {
		background: #f5f5f5;
	}
	
	::-webkit-scrollbar-thumb {
		background: #c1c1c1;
		border-radius: 4px;
	}
	
	::-webkit-scrollbar-thumb:hover {
		background: #a8a8a8;
	}
	/* #endif */
	
	/* #ifdef APP-PLUS */
	/* APP端安全区域适配 */
	.safe-area-inset-bottom {
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}
	
	.safe-area-inset-top {
		padding-top: constant(safe-area-inset-top);
		padding-top: env(safe-area-inset-top);
	}
	
	/* 确保APP端页面根元素应用样式 */
	uni-page-body {
		background-color: #f5f5f5 !important;
		color: #333333 !important;
	}
	
	/* 确保APP端页面容器应用样式 */
	.uni-page-body {
		background-color: #f5f5f5 !important;
		color: #333333 !important;
	}
	/* #endif */

	/*每个页面公共css */
	:root {
		/* 主题色 */
		--primary-color: #9b8b42;
		--text-color: #ffffff;
		--text-secondary: rgba(255, 255, 255, 0.6);
		--text-tertiary: rgba(255, 255, 255, 0.4);
		--bg-color: #000000;
		--card-bg: rgba(255, 255, 255, 0.1);
		--border-color: rgba(255, 255, 255, 0.1);
		--modal-bg: #1a1a1a;
	}
	
	page {
		background-color: var(--bg-color);
		color: var(--text-color);
		font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei', sans-serif;
	}
	
	/* 去除按钮默认边框 */
	button {
		border: none;
	}
	
	button::after {
		border: none;
	}
</style>
